/*
 * =====================================================================================
 *
 *       Filename:  test_file.cxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  04/23/2011 09:24:48 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#include <unistd.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <functional>
#include "cl_thread.hxx"

using namespace neo;
using namespace std;

static FILE* fp = NULL;
static void initLogFile(void)
{
	fp = fopen("file.log", "a+");
	if(fp == NULL)
	{
	  cout << "file.log open failed, exiting....\n";
		exit(1);
	}
}

static void thr1WriteLog(void)
{
	sleep(1);
  const char* log1 = "log1 log1 log1 log1 log1 log1 log1\n";
  for(;;)
	{
    fwrite(log1, 1, strlen(log1), fp);
		fflush(fp);
	}
}

static void thr2WriteLog(void)
{
	sleep(1);
	const char* log2 = "log2 log2 log2 log2 log2 log2 log2\n";
	for(;;)
	{
    fwrite(log2, 1, strlen(log2), fp);
		fflush(fp);
	}
}

static void thr3WriteLog(void)
{
	sleep(1);
  const char* log3 = "log3 log3 log3 log3 log3 log3 log3\n";
	for(;;)
	{
    fwrite(log3, 1, strlen(log3), fp);
		fflush(fp);
	}
}

int main(int argc, char* argv[])
{
   initLogFile();
	 CLThread thr1(bind(thr1WriteLog));
	 CLThread thr2(bind(thr2WriteLog));
	 CLThread thr3(bind(thr3WriteLog));

	 thr1.start();
	 thr2.start();
	 thr3.start();
	 thr1.join();
	 thr2.join();
	 thr3.join();

	 return 0;
}
